import React, {Component} from "react";
import {gameCompanyCol, gamePlatformCol, gameTypeCol, pageCategoryCol} from "@/js/columns";
import {Form, Modal} from "antd";
import {
    addGameCompany,
    addGameType,
    addPageType,
    deleteCompanyType,
    deleteGameType,
    getGameCompanyList,
    getGameTypeList,
    getPageCategory,
    updateGameCompany,
    updateGameType,
    updatePageType
} from "@/util/Api";
import {errorMsg, successMsg, uuid} from "@/util/global";
import {deleteIds as deletePageType} from "@/api/artPageCategory";

import GameTypeForm from "@/component/Mine/AdminConsole/OtherControl/GameTypeForm";
import PageTypeForm from "@/component/Mine/AdminConsole/OtherControl/PageTypeForm";
import JTable from "@/common/Table";
import GameCompanyForm from "@/component/Mine/AdminConsole/OtherControl/GameCompanyForm";
import {addGamePlatform, deleteGamePlatform, getGamePlatformList, updateGamePlatform} from "@/api/JustGamePlatform";
import GamePlatformForm from "@/component/Mine/AdminConsole/OtherControl/GamePlatformForm";
import Confirm from "@/common/Modal/Confirm";


class Category extends Component {
    form = React.createRef();

    constructor(props) {
        super(props);
        this.handleClickAdd = this.handleClickAdd.bind(this);
        this.handleAddOk = this.handleAddOk.bind(this);
        this.handleCancel = this.handleCancel.bind(this);
        this.handleClickEdit = this.handleClickEdit.bind(this);
        this.handleShowConfirm = this.handleShowConfirm.bind(this);
        this.handleLoad = this.handleLoad.bind(this);
        this.handleDelete = this.handleDelete.bind(this);
        let type = 0;
        let col = null, data = [];
        switch (this.props.route.label) {
            case "文章类型":
                type = 1;
                col = pageCategoryCol(this);
                break;
            case "游戏类型":
                type = 2;
                col = gameTypeCol(this);
                break;
            case "游戏厂商、工作室信息管理":
                type = 3;
                col = gameCompanyCol(this);
                break;
            case "游戏平台信息管理":
                type = 4;
                col = gamePlatformCol(this);
                break;
            default:
                break;
        }

        this.state = {
            formTitle: "表单",
            confirmVisible0: false,
            confirmVisible: false,
            showType: type,
            columns: col,
            TableLoading: false,
            selectedRowKeys: [],
            selectedRowValues: [],
            selectedIds: [],
            selectedId: null,
            data,
            disableClick: false,
            ModalVisible: false,
            formData: {},
            fileList: [],
            selectId: '',
            fileItems: [],
        }

    }

    /* 加载文章类型数据 */
    loadPageTypeData(params) {
        getPageCategory(params).then(res => {
            if (res.success) {
                this.setState({
                    data: res.data
                })
            }
        })
    }

    /* 加载游戏类型数据 */
    loadGameTypeData(params) {
        getGameTypeList(params).then(res => {
            if (res.success) {
                this.setState({
                    data: res.data
                })
            }
        })
    }

    /* 加载游戏平台数据 */
    loadGamePlatformData(params) {
        getGamePlatformList(params).then(res => {
            if (res.success) {
                this.setState({
                    data: res.data
                })
            }
        })
    }

    /* 加载游戏厂商工作室数据 */
    loadGameCompanyData(params) {
        getGameCompanyList(params).then(res => {
            if (res.success) {
                this.setState({
                    data: res.data
                })
            }
        })
    }

    /* 处理数据加载操作 */
    handleLoad(params) {
        switch (this.state.showType) {
            case 1:
                this.loadPageTypeData(params);
                break;
            case 2:
                this.loadGameTypeData(params);
                break;
            case 3:
                this.loadGameCompanyData(params);
                break;
            case 4:
                this.loadGamePlatformData(params);
                break;
        }
    }

    handleClickAdd() {
        this.setState({
            formTitle: "新增",
            ModalVisible: !this.state.ModalVisible
        });
    }

    handleShowConfirm(id) {
        if (id) {
            this.setState({
                selectId: id,
                confirmVisible: true
            })
        } else {
            this.setState({
                confirmVisible0: true
            })
        }
    }

    /* 处理删除操作 */
    handleDelete(params) {
        let func;
        switch (this.state.showType) {
            case 1:
                func = deletePageType;
                break;
            case 2:
                func = deleteGameType;
                break;
            case 3:
                func = deleteCompanyType;
                break;
            case 4:
                func = deleteGamePlatform;
                break;
        }
        func(params)
            .then(res => {
                if (res.success) {
                    let params = {
                        page: this.refs.childTable.state.page,
                        size: this.refs.childTable.state.size
                    };
                    this.handleLoad(params);
                    this.handleCancel();
                    successMsg("操作成功！");
                }
            })
    }

    handleClickEdit(data) {
        if (data.imageUrl) {
            let imgItem = {
                uid: uuid(),
                name: data.imageUrl,
                status: 'done',
                url: data.imageUrl,
                percent: 100, //100表示上传完成
            }
            this.setState({
                fileItems: [imgItem]
            })
        }
        this.form.current.setFieldsValue(data)
        this.setState({
            formTitle: "编辑",
            formData: data,
            ModalVisible: true
        });
    }

    /* 保存数据 */
    handleAddOk() {
        this.form.current.validateFields().then(r => {
            let func;
            let formData = {
                ...this.state.formData,
            }
            this.state.fileItems.length > 0 ? formData.imageUrl = this.state.fileItems[0]['imgUrl'] : null;
            if (this.state.formTitle === '新增') {
                switch (this.state.showType) {
                    case 1:
                        func = addPageType;
                        break;
                    case 2:
                        func = addGameType;
                        break;
                    case 3:
                        func = addGameCompany;
                        break;
                    case 4:
                        func = addGamePlatform;
                        break;
                }
            } else if (this.state.formTitle === '编辑') {
                switch (this.state.showType) {
                    case 1:
                        func = updatePageType;
                        break;
                    case 2:
                        func = updateGameType;
                        break;
                    case 3:
                        func = updateGameCompany;
                        break;
                    case 4:
                        func = updateGamePlatform;
                        break;
                }
            } else return;
            func(formData)
                .then(res => {
                    if (res.success) {
                        successMsg("操作成功！");
                        let params = {
                            page: this.refs.childTable.state.page,
                            size: this.refs.childTable.state.size
                        };
                        this.handleLoad(params);
                        this.handleCancel();
                    }
                })
        }).catch(err => {
            console.log(err)
            errorMsg("表单填写不正确，请确认填写无误后再提交！");
        })

    }

    handleCancel() {
        /* 重置表单数据 */
        this.form.current.resetFields()
        this.setState({
            formData: {},
            shouldUpdate: true,
            fileItems: [],
            ModalVisible: false,
            confirmVisible0: false,
            confirmVisible: false,
        });
    }

    render() {
        const {data, ModalVisible} = this.state;
        return <>
            {/* 单选删除弹窗 */}
            <Confirm visible={this.state.confirmVisible}
                     dataSize={1}
                     onOk={() => this.handleDelete({ids: [this.state.selectId]})}
                     onCancel={() => this.setState({confirmVisible: false})}
            />
            <JTable ref={"childTable"}
                    data={data}
                    columns={this.state.columns}
                    onReload={this.handleLoad}
                    page={this.state.page}
                    size={this.state.size}
                    onDelete={this.handleDelete}
                    clickSaveCallback={this.handleClickAdd}
            />
            <Modal
                forceRender={true}
                title={this.state.formTitle}
                visible={ModalVisible}
                onOk={this.handleAddOk}
                onCancel={this.handleCancel}
            >
                <Form
                    ref={this.form}
                    labelCol={{span: 4}}
                    wrapperCol={{span: 14}}
                    layout="horizontal"
                >
                    {
                        (() => {
                                switch (this.state.showType) {
                                    case 1:
                                        return <PageTypeForm that={this}/>
                                    case 2:
                                        return <GameTypeForm that={this}/>
                                    case 3:
                                        return <GameCompanyForm that={this}/>
                                    case 4:
                                        return <GamePlatformForm that={this}/>
                                    default:
                                        return null;
                                }
                            }
                        )()
                    }
                </Form>
            </Modal>
        </>
    }
}

export default Category;
